/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.service.impl;

import me.zhengjie.domain.TrainSxcjmn;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.zhengjie.service.TrainSxcjmnService;
import me.zhengjie.domain.vo.TrainSxcjmnQueryCriteria;
import me.zhengjie.mapper.TrainSxcjmnMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import me.zhengjie.utils.PageUtil;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;

/**
* @description 服务实现
* @author huangwencong
* @date 2024-10-05
**/
@Service
@RequiredArgsConstructor
public class TrainSxcjmnServiceImpl extends ServiceImpl<TrainSxcjmnMapper, TrainSxcjmn> implements TrainSxcjmnService {

    private final TrainSxcjmnMapper TrainSxcjmnMapper;

    public TrainSxcjmn findOne(Long trainId, Long trainItemId) {
        return TrainSxcjmnMapper.findOne(trainId, trainItemId);
    }
    @Override
    public PageResult<TrainSxcjmn> queryAll(TrainSxcjmnQueryCriteria criteria, Page<Object> page){
        return PageUtil.toPage(TrainSxcjmnMapper.findAll(criteria, page));
    }

    @Override
    public List<TrainSxcjmn> queryAll(TrainSxcjmnQueryCriteria criteria){
        return TrainSxcjmnMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(TrainSxcjmn resources) {
        save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(TrainSxcjmn resources) {
        TrainSxcjmn TrainSxcjmn = getById(resources.getId());
        TrainSxcjmn.copy(resources);
        saveOrUpdate(TrainSxcjmn);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Long> ids) {
        removeBatchByIds(ids);
    }

    @Override
    public void download(List<TrainSxcjmn> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (TrainSxcjmn TrainSxcjmn : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("训练 ID", TrainSxcjmn.getTrainId());
            map.put("训练项目", TrainSxcjmn.getTrainItemId());
            map.put("相对运动性错觉_顺时针60(自我空间感觉|训练持续时间|植物神经反应)", TrainSxcjmn.getCjssz60());
            map.put("相对运动性错觉_逆时针60(自我空间感觉|训练持续时间|植物神经反应)", TrainSxcjmn.getCjnsz60());
            map.put("自动运动性错觉_固定激光点(自我空间感觉|训练持续时间|植物神经反应)", TrainSxcjmn.getCjgdjgd());
            map.put("知识掌握(0：熟练掌握，1：掌握，2：了解：3：不了解)", TrainSxcjmn.getZszw());
            map.put("评定结果(0:优秀，1：良好，2：合格，3：不合格：4：复训合格)", TrainSxcjmn.getResult());
            map.put("建议", TrainSxcjmn.getJy());
            map.put("记录人", TrainSxcjmn.getCreateBy());
            map.put("记录日期", TrainSxcjmn.getDate());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}